package org.edu.modules.act.controller;

import org.activiti.engine.ProcessEngines;
import org.activiti.engine.RepositoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.edu.common.api.vo.Result;
import org.edu.modules.act.service.ModelService;
import org.edu.modules.act.common.result.ActionResult;
import org.edu.modules.act.common.result.ListResult;
import org.edu.modules.act.common.result.ResultBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.xml.stream.XMLStreamException;
import java.io.IOException;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 模型信息的控制层
 *
 * @版权所有 福州土著人信息科技有限公司
 */
@Api(tags="流程设计模块")
@RestController
@RequestMapping("/activiti/model")
public class ModelController {

	@Autowired
	private ModelService modelService;


	/**
	 * 查询模型分页
	 *
	 * @param currentPage 当前页数
	 * @param pageSize    每页记录数
	 * @param name        模型名称
	 * @return
	 */
	@ApiOperation(value="查询模型分页", notes="流程设计模块-查询模型分页")
	@GetMapping(path = "/queryModel")
	public ListResult<Object> queryModel(Integer currentPage,Integer pageSize,String name) {
		LinkedHashMap<String, Object> data = modelService.queryModel(currentPage, pageSize, name);
		return ResultBuilder.buildListSuccess(data);
	}

	/**
	 * 新增模型
	 *
	 * @param name        模型名称
	 * @param category    模型类别
	 * @param description 模型描述
	 * @return
	 */
	@PostMapping(path = "/addModel")
	@ApiOperation(value="新增模型", notes="流程设计模块-新增模型")
	public ActionResult addModel(@RequestParam(name = "name", required = true) String name, @RequestParam(name = "category", required = false) String category,
			@RequestParam(name = "description", required = false) String description) {
		modelService.addModel(name, category, description);
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 编辑模型
	 *
	 * @param modelId  模型ID
	 * @param name     模型名称
	 * @param category 模型类别
	 * @return
	 * @throws IOException
	 */
	@PutMapping(path = "/updateModel")
	@ApiOperation(value="编辑模型", notes="流程设计模块-编辑模型")
	public ActionResult updateModel(@RequestParam(name = "modelId", required = true) String modelId, @RequestParam(name = "name", required = true) String name,
			@RequestParam(name = "category", required = false) String category) throws IOException {
		modelService.updateModel(modelId, name, category);
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 * 部署模型
	 *
	 * @param modelId 模型ID
	 * @return
	 * @throws XMLStreamException
	 * @throws IOException
	 */
	@PostMapping(path = "/deployModel")
	@ApiOperation(value="部署模型", notes="流程设计模块-部署模型")
	public ActionResult deployModel(@RequestParam(name = "modelId", required = true) String modelId) throws IOException, XMLStreamException {
		modelService.deployModel(modelId);
		return ResultBuilder.buildActionSuccess();
	}

	/**
	 *
	 * 查询模型分类
	 *
	 */

	@PostMapping(path = "/getModelType")
	@ApiOperation(value="模型分类", notes="模型分类")
	public  Result<?> getModelType(String getModel){
		Map<String, Object> map = modelService.getModelType();
		return Result.OK(map);
	}

	/**
	 * 删除模型
	 *
	 * @param modelId 模型ID
	 * @return
	 */
	@PostMapping(path = "/deleteModel")
	@ApiOperation(value="删除模型", notes="流程设计模块-删除模型")
	public ActionResult deleteModel(@RequestParam(name = "modelId", required = true) String[] modelId) {
		modelService.deleteModel(modelId);
		return ResultBuilder.buildActionSuccess();
	}

}
